﻿using System.Text;

namespace InterrogazioneWebServicesBPM
{
    public abstract class AbstractBusinessDelegate
    {
        /// <summary>
        /// 
        /// </summary>
        protected string userNameValue;
        /// <summary>
        /// 
        /// </summary>
        protected string passwordText;

        protected string realmDescription;
        protected string applicationName;
        protected string applicationCredential;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sslUser">input param for SSL Connection with WebSeal BPM</param>
        /// <param name="userName">input param for Usernametoken Authentication</param>
        /// <param name="realmDescription">input param for "BANCA del gruppo"</param>
        /// <param name="applicationName"></param>
        /// <param name="applicationCredential"></param>
        /// 
        public AbstractBusinessDelegate(string sslUser, string userName, string realmDescription, string applicationName, string applicationCredential, string password)
        {
            this.initInstance(sslUser);
            this.applicationCredential = applicationCredential;
            this.applicationName = applicationName;
            this.realmDescription = realmDescription;
            //costruzione dello userNameToken 
            this.userNameValue = this.setUsernameValue(userName, applicationName, realmDescription);
            //e PasswordText
            this.passwordText = this.setPasswordText(password, applicationCredential);               
        }
        protected abstract void initInstance(string sslUser);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="applicationName"></param>
        /// <param name="realmDescription"></param>
        /// <returns>[USER].[APPLICATION]@[REALM_DESCRIPTION]</returns>
        private string setUsernameValue(string userName, string applicationName, string realmDescription)
        {
            return new StringBuilder(userName).Append(".").Append(applicationName).Append("@").Append(realmDescription).ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pwd"></param>
        /// <param name="appCredential"></param>
        /// <returns></returns>
        private string setPasswordText(string pwd, string appCredential)
        {
            StringBuilder sb = new StringBuilder(appCredential);
            if ((pwd != null) && (!"".Equals(pwd)))
            {
                sb.Insert(0, pwd);
                sb.Insert(pwd.Length, ".");
            }
            return sb.ToString();
        }
    }
}
